<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改问卷')" />
    <style>
    	.droppable-active{background-color:#ffe!important}.tools a{cursor:pointer;font-size:80%} .draggable{cursor:move}
	</style>
</head>
<body class="white-bg">
	<form class="form-horizontal m" id="form-questionnaire-edit" th:object="${questionnaire}">
            <input name="questionnaireId" th:field="*{questionnaireId}" type="hidden">
            <input name="questionnaireName" th:field="*{questionnaireName}" type="hidden">
            <input name="categoryId" th:field="*{categoryId}" type="hidden">
            <input name="isShowNo" th:field="*{isShowNo}" type="hidden">
    </form>
	<div class="div_questionnaire clearfix">
    	<div class="div_questionnaire_nav">
    		<ul class="nav_question_list">
    			<li>
	    			<div class="draggable">
	             		<div class="div_preview" data-tpl="tmpl01">单选题</div>
	             	</div>
    			</li>
    			<li>
	    			<div class="draggable">
	             		<div class="div_preview" data-tpl="tmpl02">单选组</div>
	             	</div>
    			</li>
    			<li>
	    			<div class="draggable">
	             		<div class="div_preview" data-tpl="tmpl01">复选题</div>
	             	</div>
    			</li>
    			<li>
	    			<div class="draggable">
	             		<div class="div_preview" data-tpl="tmpl02">填空题</div>
	             	</div>
    			</li>
    			<li>
	    			<div class="draggable">
	             		<div class="div_preview" data-tpl="tmpl01">判断题</div>
	             	</div>
    			</li>
    			<li>
	    			<div class="form-group draggable">
	             		<div class="div_preview" data-tpl="tmpl_pdz">判断组</div>
	             	</div>
    			</li>
    		</ul>
    	</div>
    	<div class="div_questionnaire_edit editContainBox" id="sur">
    		<div class="div_questionnaire_title">
    			<h1 th:text="${questionnaire.questionnaireName}"></h1>
    		</div>
    		<div class="form-body">
	    		<div class="div_questionnaire_edit_list droppable sortable"></div>
		    </div>
		    <button type="submit" class="btn btn-warning" data-clipboard-text="testing" id="copy-to-clipboard">复制代码</button>
   		</div>
	</div>
    <th:block th:include="include :: footer" />
    <script th:src="@{/js/jquery-ui-1.10.4.min.js}"></script>
    <script th:src="@{/js/jquery.tmpl.js}"></script>
    <script th:src="@{/ajax/libs/beautifyhtml/beautifyhtml.js}"></script>
    <script type="text/javascript">
       $(document).ready(function(){setup_draggable();$("#n-columns").on("change",function(){var v=$(this).val();if(v==="1"){var $col=$(".form-body .col-md-12").toggle(true);$(".form-body .col-md-6 .draggable").each(function(i,el){$(this).remove().appendTo($col)});$(".form-body .col-md-6").toggle(false)}else{var $col=$(".form-body .col-md-6").toggle(true);$(".form-body .col-md-12 .draggable").each(function(i,el){$(this).remove().appendTo(i%2?$col[1]:$col[0])});$(".form-body .col-md-12").toggle(false)}});$("#copy-to-clipboard").on("click",function(){var $copy=$(".form-body").clone().appendTo(document.body);$copy.find(".tools, :hidden").remove();$.each(["draggable","droppable","sortable","dropped","ui-sortable","ui-draggable","ui-droppable","form-body"],function(i,c){$copy.find("."+c).removeClass(c).removeAttr("style")});var html=html_beautify($copy.html());$copy.remove();$modal=get_modal(html).modal("show");$modal.find(".btn").remove();$modal.find(".modal-title").html("复制HTML代码");$modal.find(":input:first").select().focus();return false})});var setup_draggable=function(){$(".draggable").draggable({appendTo:"body",helper:"clone"});$(".droppable").droppable({accept:".draggable",helper:"clone",hoverClass:"droppable-active",drop:function(event,ui){$(".empty-form").remove();var $orig=$(ui.draggable);if(!$(ui.draggable).hasClass("dropped")){var $el=$orig.clone().addClass("dropped").addClass("div_question").css({"position":"static","left":null,"right":null}).appendTo(this);
       var tplid = $el.find(".div_preview").data("tpl");
       data = $.parseJSON('{"l":[{"i":false,"it":"1","no":1,"t":"选项1","td":false},{"i":false,"it":"1","no":2,"t":"选项2","td":false}],"no":1,"qc":"这是一个判断组","qs":"01","qt":"pdz"}');
      //alert($("#"+tplid).html());
       $("#"+tplid).tmpl(data).appendTo($el.find(".div_preview"));
       if($el.find("label").hasClass("radio-box")){$el=$el.html('<label class="col-sm-3 control-label">单选框：</label>'+'<div class="col-sm-9">'+'<label class="radio-box"><input type="radio" checked="" value="option1" id="optionsRadios1" name="optionsRadios">选项1</label>'+'<label class="radio-box"><input type="radio" value="option2" id="optionsRadios2" name="optionsRadios">选项2</label>'+"</div>")}else{if($el.find("label").hasClass("check-box")){$el=$el.html('<label class="col-sm-3 control-label">复选框：</label>'+'<div class="col-sm-9">'+'<label class="check-box">'+'<input type="checkbox" value="option1" id="inlineCheckbox1">选项1</label>'+'<label class="check-box">'+'<input type="checkbox" value="option2" id="inlineCheckbox2">选项2</label>'+'<label class="check-box">'+'<input type="checkbox" value="option3" id="inlineCheckbox3">选项3</label>'+"</div>")}}
       var id=$orig.find(":input").attr("id");if(id){id=id.split("-").slice(0,-1).join("-")+"-"+(parseInt(id.split("-").slice(-1)[0])+1);$orig.find(":input").attr("id",id);$orig.find("label").attr("for",id)}$('<p class="tools"><a class="edit-link">编辑HTML<a>|<a class="remove-link">移除</a></p>').appendTo($el)}else{if($(this)[0]!=$orig.parent()[0]){var $el=$orig.clone().css({"position":"static","left":null,"right":null}).appendTo(this);$orig.remove()}}}}).sortable()};var get_modal=function(content){var modal=$('<div class="modal" style="overflow: auto;" tabindex="-1">	<div class="modal-dialog"><div class="modal-content"><div class="modal-header"><a type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</a><h4 class="modal-title">编辑HTML</h4></div><div class="modal-body ui-front">	<textarea class="form-control textarea-show-src"  style="min-height: 200px; margin-bottom: 10px;font-family: Monaco, Fixed"></textarea><button class="btn btn-success">更新HTML</button></div></div></div></div>').appendTo(document.body);var doms=document.getElementsByClassName("textarea-show-src");for(var i=0;i<doms.length;i++){doms.item(i).innerHTML=content}return modal};$(document).on("click",".edit-link",function(ev){var $el=$(this).parent().parent();var $el_copy=$el.clone();var $edit_btn=$el_copy.find(".edit-link").parent().remove();var $modal=get_modal(html_beautify($el_copy.html())).modal("show");$modal.find(":input:first").focus();$modal.find(".btn-success").click(function(ev2){var html=$modal.find("textarea").val();if(!html){$el.remove()}else{$el.html(html);$edit_btn.appendTo($el)}$modal.modal("hide");return false})});$(document).on("click",".remove-link",function(ev){$(this).parent().parent().remove()});
    </script>
    <script th:inline="javascript">
        var prefix = ctx + "qa/questionnaire";
        $("#form-questionnaire-edit").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                $.operate.save(prefix + "/edit", $('#form-questionnaire-edit').serialize());
            }
        }
        
        $(document).on("click",".qa-remove-link",function(ev){
        	//$(this).parent().parent().remove();
        	alert($(this).parent().parent().parent().parent().parent().html());
        	$(this).parent().parent().parent().parent().parent().remove();
        });
    </script>
</body>
</html>

<script id="tmpl_pdz" type="text/x-jquery-tmpl">
<div class="qa_edit_content">
	<div class="qa_q_show_title">
		<span></span>
	<div>
	<div class="qa_q_show_answer">
		<table>
			<tr><td colspan="2">${data.qc}</td><td width="25px">是</td><td width="25px">否</td></tr>
			{{each(i,a) data.l}}
				<tr><td width="40px;">${i+1}</td><td>${a.t}</td><td><input type="radio" value="${a.t}-是" name="${data.no}_${a.no}" /></td><td><input type="radio" value="${a.t}-否" name="${data.no}_${a.no}" /></td></tr>
			{{/each}}
		</table>
    <div>
</div>
</script>